package com.cpkso.buss.backend.user.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.util.StringUtils;

import com.runze.base.domain.UserEntity;

/**
 * 用户表：user
 * 
 * 不需要存储卖家买家号的id或者名
 * 订单保存用户id
 * 
 * @author wrzhxy@qq.com
 * @date 2018年3月13日
 */
@Entity
@Table(name="user"
	, indexes={@Index(name="idx_user_username", columnList="name")})
	@Cache(usage= CacheConcurrencyStrategy.READ_WRITE)
public class User extends UserEntity {
	// 用户名称
	@Basic(optional=false)
	private String name;
	// 密码
	@Basic(optional=false)
	private String password;
	// 盐值
	@Basic(optional=false)
	private String salt;
	// 是主管吗 0.55 0.5
	@Basic(optional=false)
	private Double charge;
	
	@ManyToMany(fetch=FetchType.LAZY)
//	@JoinTable(name="user_role", joinColumns=@JoinColumn(name="uid", foreignKey=@ForeignKey(name = "FK_ur_user"))
//		, inverseJoinColumns=@JoinColumn(name="rid"), foreignKey=@ForeignKey(name = "FK_ur_role"))
	@JoinTable(name="user_role", joinColumns=@JoinColumn(name="uid", foreignKey=@ForeignKey(name = "FK_ur_user"))
	, inverseJoinColumns=@JoinColumn(name="rid", foreignKey=@ForeignKey(name = "FK_ur_role")))
	@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
	private Set<Role> roles = new HashSet<>();
	
	public User() {
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = StringUtils.hasText(name) ? name.trim() : null;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSalt() {
		return salt;
	}
	public void setSalt(String salt) {
		this.salt = salt;
	}
	public Double getCharge() {
		return charge;
	}
	public void setCharge(Double charge) {
		this.charge = charge;
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	public void addRole(Role role) {
		this.roles.add(role);
	}
	public void removeRole(Role role) {
		this.roles.remove(role);
	}
	
}
